﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESpace;

namespace ESpace_Project
{
    public partial class FormBinRW : Form
    {
        public FormBinRW()
        {
            InitializeComponent();
        }

        Random rnd = new Random();

        private void button3_Click(object sender, EventArgs e)
        {
            im.Image = new Bitmap(im.Width, im.Height);
            Graphics gr = Graphics.FromImage(im.Image);
            for (int i = 0; i < im.Width; i++)
                for (int j = 0; j < im.Height; j++)
                    ((Bitmap)im.Image).SetPixel(i, j, Color.FromArgb(rnd.Next()));
            im.Refresh();
        }


        List<CheckBox> chbxL = new List<CheckBox>();
        private void button4_Click(object sender, EventArgs e)
        {
            CheckBox chbx = new CheckBox();
            chbx.Text = chbxL.Count.ToString();
            chbx.Width = 40;
            chbxL.Add(chbx);

            panel1.Controls.Clear();
            panel1.HorizontalScroll.Value = 0;
            for (int i = 0; i < chbxL.Count; i++)
            {
                chbxL[i].Location = new Point(i * 40, 0);
                panel1.Controls.Add(chbxL[i]);
            }
        }

        byte[] buf;

        private void button1_Click(object sender, EventArgs e)
        {
            BinWriter bw = new BinWriter();
            bw.WriteBmp((Bitmap)im.Image);
            bw.WriteString(textBox1.Text);

            bw.WriteShort(Convert.ToInt16(textBox2.Text));
            bw.WriteInt(Convert.ToInt32(textBox3.Text));
            bw.WriteLong(Convert.ToInt64(textBox4.Text));

            bool[] b = new bool[chbxL.Count];
            for(int i=0;i<chbxL.Count;i++)
                b[i]=chbxL[i].Checked;
            bw.WriteBitMas(b);

            bw.WriteFloat(Convert.ToSingle(textBox5.Text));
            bw.WriteDouble(Convert.ToDouble(textBox6.Text));

            buf = bw.buf;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            BinReader br = new BinReader(buf);
            im1.Image = br.ReadBmp(); im1.Refresh();
            textBox12.Text = br.ReadString();

            textBox11.Text = br.ReadShort().ToString();
            textBox10.Text = br.ReadInt().ToString();
            textBox9.Text = br.ReadLong().ToString();

            panel2.Controls.Clear();
            bool[] b = br.ReadBitMas();
            for (int i = 0; i < b.Length; i++)
            {
                CheckBox ch = new CheckBox();
                ch.Location = new Point(i*20,0);
                ch.Width = 20;
                ch.Checked = b[i];
                panel2.Controls.Add(ch);
            }

            textBox8.Text = br.ReadFloat().ToString();
            textBox7.Text = br.ReadDouble().ToString();

        }
    }
}
